trigger AttendeeTrigger on MeetingAttendees__c (before insert, before update) {
	Map<String, Id> uIds = new Map<String, Id>();
	Map<String, Id> cIds = new Map<String, Id>();
	Map<String, Id> lIds = new Map<String, Id>();
	
	for (MeetingAttendees__c mi : trigger.new) {
		if (mi.User__c != null) {
			uIds.put(mi.User__c, mi.User__c);
		} else if (mi.Lead__c != null) {
			lIds.put(mi.Lead__c, mi.Lead__c);
		} else if (mi.Contact__c != null) {
			cIds.put(mi.Contact__c, mi.Contact__c);
		}
	}
	
	Map<Id, User> uEm = new Map<Id, User>([Select Id, Email From User Where Id In :uIds.keySet()]);
	Map<Id, Contact> cEm = new Map<Id, Contact>([Select Id, Email From Contact Where Id In :cIds.keySet()]);
	System.debug(LoggingLevel.INFO, '\n\nContat list: ' + cEm + '\n');
	
	Map<Id, Lead> lEm = new Map<Id, Lead>([Select Id, Email From Lead Where Id In :lIds.keySet()]);

	for (MeetingAttendees__c ma : trigger.new) {
		if (ma.User__c != null) {
			ma.Email__c = uEm.get(ma.User__c).Email;
			ma.LinkId__c = ma.User__c;
		} else if (ma.Lead__c != null) {
			ma.Email__c = lEm.get(ma.Lead__c).Email;
			ma.LinkId__c = ma.Lead__c;
		} else if (ma.Contact__c != null) {
			ma.Email__c = cEm.get(ma.Contact__c).Email;
			ma.LinkId__c = ma.Contact__c;
		}
	}
}